package com.example.leetdemo.process;

import java.util.HashMap;
import java.util.Map;

public class Solution290 {


    public boolean wordPattern(String pattern, String s) {

        Map<Character, String> str = new HashMap<>();
        Map<String, Character> check = new HashMap<>();
        String[] t = s.split(" ");
        if (pattern.length() != t.length) {
            return false;
        }
        for (int i = 0; i < t.length; i++) {
            if (!str.containsKey(pattern.charAt(i))) {
                str.put(pattern.charAt(i), t[i]);
                if (!check.containsKey(t[i])) {
                    check.put(t[i], pattern.charAt(i));
                } else if (!check.get(t[i]).equals(pattern.charAt(i))) {
                    return false;
                }
            } else {
                if (!str.get(pattern.charAt(i)).equals(t[i])) {
                    return false;
                }
            }
        }
        return true;
    }

}
